package com.enjoyor.soa.traffic.server.efoms.dubbo.listener;

import com.enjoyor.soa.traffic.api.dto.idcs.BayonetStatusDto;
import com.enjoyor.soa.traffic.api.service.dubbo.idcs.IDCSCallbackListener;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceDetailInfo;
import com.enjoyor.soa.traffic.core.efoms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.server.efoms.common.Global;
import com.enjoyor.soa.traffic.server.efoms.dubbo.invoke.DubboBayonetCallbackServiceInvoke;
import com.enjoyor.soa.traffic.server.efoms.dubbo.manage.ManageDevWranHandleService;
import com.enjoyor.soa.traffic.server.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

@Async
@Order(1)
@Component
public class DubboBayonetCallBackListener implements CommandLineRunner {
    String listenerId = "fffffffffff";
    
    @Autowired
    private DubboBayonetCallbackServiceInvoke bayonetCallbackInvoke;
	@Autowired
    private ManageDevWranHandleService handleService;
    
    String typeCode = EnumDicPublic.FASTENER.getKey();
	String typeName = EnumDicPublic.FASTENER.getValue();
    
    @Override
    public void run(String... args) throws Exception {
        //startCallBack();
    }

    private void startCallBack() throws InterruptedException {
        addListener(listenerId);
        while (true) {
            try {
                if (bayonetCallbackInvoke != null && !bayonetCallbackInvoke.BayonetStatusHeartBeat(listenerId)) {
                    addListener(listenerId);
                }
            } catch (Exception e) {
                StackTraceElement StackTrace = Thread.currentThread().getStackTrace()[1];
                //LogInfoHelper.writeErrorLogInfo("设备运维回调", StackTrace, e);
            } finally {
                Thread.sleep(10000);
            }
        }
    }

    IDCSCallbackListener listener;
    private IDCSCallbackListener getListener() {
        if (listener == null) {
            listener = new IDCSCallbackListener() {
                @Override
                @SuppressWarnings("unchecked")
                public void changed(ResultPojo msg) {
                    handler.handle(msg); 
                }
            };
        }
        return listener;
    }

    private void addListener(String name) {
        try {
        	bayonetCallbackInvoke.addBayonetStatusListener(name, getListener());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    BasicMsgHandler<ResultPojo> handler = new BasicMsgHandler<ResultPojo>() {
        /**
         * 获取回调，获取卡口设备状态变化数据
         * @param result
         * @return
         */
		@Override
        @SuppressWarnings("unchecked")
        protected boolean process(ResultPojo msg) {
        		try {
        			if(CheckPojoHelper.isValid(msg)) {
        				List<BayonetStatusDto> bayonetStatusList = (List<BayonetStatusDto>)msg.getResultList();
        	        	for(BayonetStatusDto bayonetStatusDto : bayonetStatusList) {
        	        		String devId = bayonetStatusDto.getBayonet_id();
        	        		String key = typeCode + "-" + devId;
        	        		DeviceDetailInfo deviceDetailInfo = Global.DeviceInfoPool.get(key);
        	        		String devName = "";
        	        		if(null != deviceDetailInfo) {
        	        			devName = deviceDetailInfo.getDevName();
        	        		}else {
        	        			continue;
        	        		}
        	        		//根据策略生成数据报警数据
        	        		String collectTime = bayonetStatusDto.getLast_pass_time();
        	        		String receiveTime = bayonetStatusDto.getLast_receive_time();
        	        		String writeDbTime = bayonetStatusDto.getLast_record_time();
        	        		String picUrl = bayonetStatusDto.getLast_vehicle_pic();//最近过车图片；
        	        		//根据策略生成数据报警
        	        		handleService.devDataWranHandle(typeCode, typeName, devId, devName, collectTime, 
        	        				receiveTime, writeDbTime,picUrl, "订阅卡口状态");
        	        	}
        			}
                } catch (Exception e) {
                    StackTraceElement StackTrace = Thread.currentThread().getStackTrace()[1];
                    //List listParam = Arrays.asList(result);
                    //LogInfoHelper.writeErrorLogInfo("处理缉查布控回调", StackTrace, listParam, e, ResultPojo.class);
                }
            return true;
        }
    };
    
}
